package pacBioSimple;

import java.util.ArrayList;
import java.util.List;

public class KKmerList {
	
	private List<StringBuilder> kKmerList=null;
	private int kmerSize=0;
	
	public KKmerList(int kmerSize)
	{
		this.kmerSize=kmerSize;
		init();
	}
	
	public List<StringBuilder> getkKmerList() {
		return kKmerList;
	}

	public void setkKmerList(List<StringBuilder> kKmerList) {
		this.kKmerList = kKmerList;
	}

	public int getKmerSize() {
		return kmerSize;
	}

	public void setKmerSize(int kmerSize) {
		this.kmerSize = kmerSize;
	}

	
	public String toString()
	{
		StringBuilder sb = new StringBuilder();
		sb.append("kmerSize: "+this.kmerSize+"\n");
		sb.append("kmerList \n");
		for(int i=0; i < kKmerList.size();++i)
		{
			sb.append(i+": "+kKmerList.get(i)+" \n");
		}
		return sb.toString();
	}
	
	private boolean checkAndModifyTemplate(int i, StringBuilder template)
	{
		boolean overflow=false;

		if(template.charAt(i)=='A')
		{
			template.setCharAt(i,'C');
			overflow=false;
		}
		else if (template.charAt(i)=='C')
		{
			template.setCharAt(i,'G');
			overflow=false;
		}
		else if (template.charAt(i)=='G')
		{
			template.setCharAt(i,'T');
			overflow=false;
		}
		else if (template.charAt(i)=='T')
		{
			template.setCharAt(i,'A');
			overflow=true;
		}
		return  overflow;
	}
	
	public void init()
	{
		kKmerList = new ArrayList<StringBuilder>();
		
		boolean beginOverflow=false;
		StringBuilder template = new StringBuilder();
		for(int i=0; i < kmerSize;++i)
		{
			template.append('A');
		}
		kKmerList.add(template);
		while(!beginOverflow)
		{	
			int i=kmerSize-1;
			while(i>=0)
			{
				if(!beginOverflow && i == kmerSize-1)
				{
					beginOverflow = checkAndModifyTemplate(i, template);
				}
				else if(beginOverflow)
				{
					beginOverflow = checkAndModifyTemplate(i, template);
				}
				else
				{
					break;
				}
				--i;
			}
			if(beginOverflow)
			{
				break;
			}
			else
			{
				kKmerList.add(new StringBuilder(template));
			}
		}
	}

}
